﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SHDocVw;
using System.Threading;
using mshtml;

namespace MQuery
{
    /// <summary>
    /// Summary description for SelectorsTest
    /// </summary>
    [TestClass]
    public class SelectorsTest
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        static HTMLDocumentClass document;

        [TestInitialize]
        public void TestInitialize()
        {
            document = MQueryUnitTest.Load("http://localhost/JQueryTest1.htm");
        }
        [TestCleanup]
        public void TestCleanup()
        {
            MQueryUnitTest.Quit();
        }

        [Owner("Yehuda Adar")]
        [TestMethod]
        public void IDSelector_SelectDocument_ReturnsIHTMLElementByID()
        {
            string id = "DivID1";
            ISelector selector = new IDSelector(id);

            IList<IHTMLElement> result = selector.Select(document);

            Assert.AreEqual(result.Count, 1);
            Assert.IsInstanceOfType(result[0], typeof(IHTMLElement));
            Assert.AreEqual(result[0].id, id);
            return;
        }

        [Owner("Yehuda Adar")]
        [TestMethod]
        [ExpectedException(typeof(NotImplementedException))]
        public void IDSelector_SelectElements_ThrowsNotImplementedException()
        {
            string id = "DivID1";
            ISelector selector = new IDSelector(id);

            IList<IHTMLElement> result = selector.Select(new List<IHTMLElement>(){document.documentElement});
        }

        [Owner("Yehuda Adar")]
        [TestMethod]
        public void ElementSelector()
        {
            string tag = "Div";
            ISelector selector = new ElementSelector(tag);

            IList<IHTMLElement> result = selector.Select(document);
            Assert.AreEqual(result.Count, document.getElementsByTagName(tag).length);

            foreach (var element in result)
            {
                Assert.IsTrue(string.Compare(((IHTMLElement)element).tagName, tag, true) == 0);
            }
        }

        [Owner("Yehuda Adar")]
        [TestMethod]
        public void ClassSelector()
        {
            string className = "Class1";

            ISelector selector = new ClassSelector(className);
            
             IList<IHTMLElement> result = selector.Select(document);
            foreach (var element in result)
            {
                Assert.IsTrue(element.HasClass(className));
            }
        }
    }
}
